#include "MySence.h"
#include "MainForm.h"
#include "textinput.h"
#include "def.h"
#include "sqlite3.h"


CCMenuItem *theSaveItem,*theDelItem,*theAddItem,*theUpdateItem;
TextInput *pInput;

CMySence::CMySence(bool bPortrait)
:m_bPortrait(bPortrait)
{
	if (m_bPortrait)
	{
		CCDirector::sharedDirector()->setDeviceOrientation(CCDeviceOrientationLandscapeRight);
	}
    
	CCScene::init();
    
	CCSize s = CCDirector::sharedDirector()->getWinSize();
	win_width = s.width;
	win_height = s.height;
    
	
    
	scale = s.width/line_count/cell_width;
	
	//set the background image
	CCRect r(0, 0, win_width, win_height);
	CCSprite* sp = CCSprite::spriteWithFile(bgfile, r);
	ccTexParams tp = {GL_LINEAR, GL_LINEAR, GL_REPEAT,GL_REPEAT};
	sp->getTexture()->setTexParameters(&tp);
	sp->setPosition(ccp(win_width/2, win_height/2));
	this->addChild(sp);
    
	//string path=CCFileUtils::getWriteablePath();
	//CCLog("path=[%s]",path);
}

void CMySence::onEnter()
{
	CCScene::onEnter();
	//CCLog("win_width=[%f]win_height=[%f]scale=[%f]",win_width,win_height,scale);
	//add bottom bar
	CCSprite *botTop=CCSprite::spriteWithFile(barfile); 
	botTop->setScale(scale);
	botTop->setPosition(ccp(win_width/2, menu_height*scale/2) );
	this->addChild(botTop, 0);
	
    
	CCSize s = CCDirector::sharedDirector()->getWinSize();
	CCSpriteBatchNode *spriteBatch=CCSpriteBatchNode::batchNodeWithFile("z.png");  
	this->addChild(spriteBatch);  
	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("z.plist"); 
	
	// add 
	CCSprite* newNormalSprite = CCSprite::spriteWithSpriteFrameName("_add.png");
	CCSprite* newSelectedSprite = CCSprite::spriteWithSpriteFrameName("_add.png");
	CCSprite* newDisabledSprite = CCSprite::spriteWithSpriteFrameName("_add.png");
	CCMenuItemSprite* newItem = CCMenuItemSprite::itemFromNormalSprite(newNormalSprite, newSelectedSprite, newDisabledSprite, this, menu_selector(CMySence::menuAddCallback) );
	newItem->setScale(scale);
	theAddItem = newItem;
    
	//save
	CCSprite* saveNormalSprite = CCSprite::spriteWithSpriteFrameName("_save.png");
	CCSprite* saveSelectedSprite = CCSprite::spriteWithSpriteFrameName("_save.png");
	CCSprite* saveDisabledSprite = CCSprite::spriteWithSpriteFrameName("_save.png");
	CCMenuItemSprite* saveItem = CCMenuItemSprite::itemFromNormalSprite(saveNormalSprite, saveSelectedSprite, saveDisabledSprite, this, menu_selector(CMySence::menuSaveCallback) );
	theSaveItem=saveItem;
	saveItem->retain();
	theSaveItem->setIsEnabled(false);
	saveItem->setScale(scale);
    
	//delete
	CCSprite* delNormalSprite = CCSprite::spriteWithSpriteFrameName("_del.png");
	CCSprite* delSelectedSprite = CCSprite::spriteWithSpriteFrameName("_del.png");
	CCSprite* delDisabledSprite = CCSprite::spriteWithSpriteFrameName("_del.png");
	CCMenuItemSprite* delItem = CCMenuItemSprite::itemFromNormalSprite(delNormalSprite, delSelectedSprite, delDisabledSprite, this, menu_selector(CMySence::menuClearCallback) );
	delItem->setScale(scale);
	theDelItem = delItem;
    
    
	// info 
	CCSprite* infoNormalSprite = CCSprite::spriteWithSpriteFrameName("_info.png");
	CCSprite* infoSelectedSprite = CCSprite::spriteWithSpriteFrameName("_info.png");
	CCSprite* infoDisabledSprite = CCSprite::spriteWithSpriteFrameName("_info.png");
	CCMenuItemSprite* infoItem = CCMenuItemSprite::itemFromNormalSprite(infoNormalSprite, infoSelectedSprite, infoDisabledSprite, this, menu_selector(CMySence::menuSynCloudCallback) );
	infoItem->setScale(scale);
    
    
	bottomMenu =CCMenu::menuWithItems(newItem,saveItem,delItem,infoItem,NULL);
	bottomMenu->alignItemsHorizontallyWithPadding(10);
	bottomMenu->setPosition( ccp(s.width/2,menu_height*scale/2) );
    addChild(bottomMenu, 1);
    
}

void CMySence::menuCloseCallback(CCObject* pSender)
{
	CCDirector::sharedDirector()->end();
}


void CMySence::menuAddCallback(CCObject* pSender)
{
	//CCScene* pScene = new CMySence();
	pInput = new TextInput();
	
	//CCMenuItemSprite* saveItem = (CCMenuItemSprite*)(bottomMenu->getChildByTag(1001));//->setIsEnabled(false);
	theSaveItem->setIsEnabled(!theSaveItem->getIsEnabled());
	theDelItem->setIsEnabled(!theDelItem->getIsEnabled());
	//theUpdateItem->setIsEnabled(!theUpdateItem->getIsEnabled());
	theAddItem->setIsEnabled(!theAddItem->getIsEnabled());
	//this->removeChildByTag(0,true);
    
	this->addChild(pInput, 0 );
	pInput->onClickTrackNode(true);
	//CCDirector::sharedDirector()->replaceScene( CCTransitionFlipX::transitionWithDuration(2, pScene) );
	//CCDirector::sharedDirector()->replaceScene(pScene);
    //   pScene->release();
    //pInput->release();
	//pInput->autorelease();
}

void CMySence::menuListCallback(CCObject* pSender)
{
	CCScene* pScene = new CMySence();
    MainForm* pLayer = new MainForm();
	pScene->addChild( pLayer, 0 );
	pLayer->sence = pScene;
    
	//CCDirector::sharedDirector()->replaceScene( CCTransitionFlipX::transitionWithDuration(2, pScene) );
	CCDirector::sharedDirector()->replaceScene(pScene );
    pScene->release();
    
    pLayer->release();
}

void CMySence::menuSynCloudCallback(CCObject* pSender)
{
	//CCLog("syn with cloud node");
    
    
	sqlite3 *pDB = NULL;
	char * msg = NULL;
	int ret;
	char * sql = "create table if not exists messages (msgid integer PRIMARY KEY autoincrement, msgtext TEXT  NULL,synflag  integer DEFAULT 0 NULL)";
    
	//如果不存在会自动创建
	ret = sqlite3_open(getdbfile(), &pDB);
	CCLog("sqlite3_open ret=%d",ret);
    
	ret = sqlite3_exec(pDB, sql, NULL, NULL, &msg);
	CCLog("exec [%s] ret=%d msg=%s",sql,ret,msg);
	
	char sqlisert[1024]={'\0'};   
	for(int i=0;i<2; i++)
	{
		char val[100] = {'\0'};
		sprintf(val,"read book",i);
		sprintf( sqlisert, "insert into messages(msgtext,synflag) values('%s',1)", val );   // pTextField->getString()默认即UTF-8无需转码
		//CCLog(sqlisert);
        
		ret = sqlite3_exec(pDB, sqlisert, NULL, NULL, &msg);
		//CCLog("exec [%s] ret=%d msg=%s",sqlisert,ret,msg);
	}
	sqlite3_close(pDB);
    
	menuListCallback(NULL);
    
}


void CMySence::menuClearCallback(CCObject* pSender)
{
	sqlite3 *pDB = NULL;
	char * msg = NULL;
	int ret;
	char * sql = "delete from messages where synflag=1";
    
	//如果不存在会自动创建
	ret = sqlite3_open(getdbfile(), &pDB);
	CCLog("sqlite3_open ret=%d",ret);
    
	ret = sqlite3_exec(pDB, sql, NULL, NULL, &msg);
	CCLog("exec [%s] ret=%d msg=%s",sql,ret,msg);
	
	sqlite3_close(pDB);
	menuListCallback(NULL);
    
}

void CMySence::menuUpdateCallback(CCObject* pSender)
{
	sqlite3 *pDB = NULL;
	char * msg = NULL;
	int ret;
	char * sql = "delete from messages";
    
	//如果不存在会自动创建
	ret = sqlite3_open(getdbfile(), &pDB);
	CCLog("sqlite3_open ret=%d",ret);
    
	ret = sqlite3_exec(pDB, sql, NULL, NULL, &msg);
	CCLog("exec [%s] ret=%d msg=%s",sql,ret,msg);
	
	sqlite3_close(pDB);
    
}

void CMySence::menuSaveCallback(CCObject* pSender)
{
	if( pInput != NULL)
	{
		pInput->onSave(NULL);
		this->removeChild(pInput,true);
		pInput->release();
	}
    
	theSaveItem->setIsEnabled(!theSaveItem->getIsEnabled());
	theDelItem->setIsEnabled(!theDelItem->getIsEnabled());
    //	theUpdateItem->setIsEnabled(!theUpdateItem->getIsEnabled());
	theAddItem->setIsEnabled(!theAddItem->getIsEnabled());
    
	menuListCallback(NULL);
}

void CMySence::menuDelCallback(CCObject* pSender)
{
	//del_switch = !del_switch;
    
}